<?php $total_invoice = 0;?>
<?php $total_debit = 0;?>
<?php $total_credit = 0;?>
<?php $total_payment = 0;?>
<?php $total_others = 0;?>
<?php $total_receivables = 0;?>
<table class="datagrid" style="width:300px;border:1px black;">
  <thead class="datagrid">
    <tr>
      <th>
        Invoice Date
      </th>
      <th>
        Invoice#
      </th>
      <th>
        Terms
      </th>
      <th>
        Due Date
      </th>
      <th>
        Invoice<br/>Amount
      </th>
      <th>
        Debit
      </th>
      <th>
        Credit
      </th>
      <th>
        Payment
      </th>
      <th>
        Others
      </th>
      <th>
        Balance
      </th>
    </tr>
  </thead>
  <tbody class="datagrid" style="height:auto;">
    <?php foreach($receivables as $receivable):?>
      <?php 
        $invoice = InvoicePeer::retrieveByPk($receivable['iden2']);
        $transaction_list = $invoice->getTransactions();
      ?>
      <?php include_partial('invoice_list_item',array('receivable'=>$receivable,'invoice'=>$invoice,'transaction_list'=>$transaction_list,))?>
      <?php $total_invoice += $invoice->getTotal();?>
      <?php $total_debit += $transaction_list['DEBIT MEMO'];?>
      <?php $total_credit += $transaction_list['CREDIT MEMO'];?>
      <?php $total_payment += $transaction_list['PAYMENT'];?>
      <?php $total_others += $transaction_list['OTHERS'];?>
      <?php $total_receivables += $receivable['total'];?>
    <?php endforeach;?>
  </tbody>
  <tfoot class="datagrid">
    <tr>
      <td style="text-align:right;" colspan="4">Total</td>
      <td style="text-align:right;"><?php echo number_format($total_invoice,2) ?></td>
      <td style="text-align:right;"><?php echo number_format($total_debit,2) ?></td>
      <td style="text-align:right;"><?php echo number_format($total_credit,2) ?></td>
      <td style="text-align:right;"><?php echo number_format($total_payment,2) ?></td>
      <td style="text-align:right;"><?php echo number_format($total_others,2) ?></td>
      <td style="text-align:right;"><?php echo number_format($total_receivables,2) ?></td>
    </tr>
  </tfoot>
</table>